package com.team.teamproject.ui.homepage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.team.teamproject.R;
import com.team.teamproject.adapter.MyHomePageBottomAdapter;
import com.team.teamproject.bean.Data_HomePageBottom;
import com.team.teamproject.utils.HttpUtils;
import com.team.teamproject.utils.ParseJson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/8.
 */
public class PagerFragment_Qita extends Fragment{
    private PullToRefreshListView mPullToRefreshListView;
    private ListView lv;
    List<Data_HomePageBottom.ItemsBean> items;
    public Handler handler = new Handler();
    private MyHomePageBottomAdapter bottomAdapter;
    //除精选外的
    // Url
    int id ;
    int limit = 20;
    String bottomUrl;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragement_homepage_pagefrageqita, null);
       // lv = (ListView) view.findViewById(R.id.lv);
        mPullToRefreshListView= (PullToRefreshListView) view.findViewById(R.id.ptlv);
        mPullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        //获取PullToRefreshListView中封装的listview
        lv=mPullToRefreshListView.getRefreshableView();
        Bundle bundle = getArguments();
        if (bundle != null) {
            id = bundle.getInt("id");
           // Log.i("aaa","其他ID--->"+id);
        }
        bottomUrl="http://api.liwushuo.com/v2/channels/"+id+"/items_v2?limit="+limit+"&ad=2&gender=1&offset=0&generation=1";
       // Log.i("aaa","其他URL--->"+bottomUrl);
        items = new ArrayList<>();
        //获取数据
        getData();
        bottomAdapter=new MyHomePageBottomAdapter(getActivity(),items);
        //刷新
        refreshData();

        return view;
    }

    private void refreshData() {
        mPullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                getData();
                bottomAdapter.notifyDataSetChanged();
                mPullToRefreshListView.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                limit+=10;
                if (limit>50){
                    mPullToRefreshListView.onRefreshComplete();
                    Toast.makeText(getActivity(), "已到达底部", Toast.LENGTH_SHORT).show();
                    return;
                }
                Log.i("aaa","上拉加载前id--->"+id);
                Log.i("aaa","上拉加载前limit--->"+limit);
                bottomUrl="http://api.liwushuo.com/v2/channels/"+id+"/items_v2?limit="+limit+"&ad=2&gender=1&offset=0&generation=1";
                //上拉刷新后
                Log.i("aaa","上拉后Url---->"+bottomUrl);
                getData();
                bottomAdapter.notifyDataSetChanged();
               mPullToRefreshListView.onRefreshComplete();
            }
        });
    }

    private void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //底部JSON
                String json = HttpUtils.getHttpUtils().getJson(bottomUrl);

                if (json!=null){
                    //------底部(listview)
                    List<Data_HomePageBottom> datas = ParseJson.fromToHomeBottom(json);
                    //获取Items中数据
                    final List<Data_HomePageBottom.ItemsBean> items1 = datas.get(0).getItems();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //底部数据
                                items.addAll(items1);
                                lv.setAdapter(bottomAdapter);
                                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                        Data_HomePageBottom.ItemsBean itemsBean = PagerFragment_Qita.this.items.get(position);
                                        int idItems = itemsBean.getId();
                                        Intent intent = new Intent();
                                        intent.putExtra("id",idItems+"");
                                        intent.setClass(getActivity(),HomePage_XiangQingActivity.class);
                                        startActivity(intent);
                                    }
                                });
                            }
                        });
                }else{
                    Toast.makeText(getActivity(), "请检查网络。。。", Toast.LENGTH_SHORT).show();
                }
            }
        }).start();
    }
}
